package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.auto.reward.UserEveryDayChargeReward;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/8/27
 */
public class UserActivityChargeAutoRewardDaoImpl extends TempDataObjectDao<UserEveryDayChargeReward> {

    public static String getUserEveryDayChargeRewardSql = "select * from t_u_every_day_charge_reward where activityId in (paramList)";

    public Map<Integer, Map<Integer, Map<Integer, Map<Long, UserEveryDayChargeReward>>>> getUserEveryDayChargeReward(List<Integer> expiredActivityBiggerThanFiveDayList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, Map<Integer, Map<Long, UserEveryDayChargeReward>>>> map = new ConcurrentHashMap<>();
        if (expiredActivityBiggerThanFiveDayList.size() == 0) {
            return map;
        }

        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserEveryDayChargeRewardSql.replace("paramList", StringUtils.listToString(expiredActivityBiggerThanFiveDayList, ",")));
            rs = call.executeQuery();
            while (rs.next()) {
                UserEveryDayChargeReward userEveryDayChargeReward = parse(rs);
                if (!map.containsKey(userEveryDayChargeReward.getActivityId())) {
                    map.put(userEveryDayChargeReward.getActivityId(), new ConcurrentHashMap<>());
                }
                if (!map.get(userEveryDayChargeReward.getActivityId()).containsKey(userEveryDayChargeReward.getTimeMark())) {
                    map.get(userEveryDayChargeReward.getActivityId()).put(userEveryDayChargeReward.getTimeMark(), new ConcurrentHashMap<>());
                }
                if (!map.get(userEveryDayChargeReward.getActivityId()).get(userEveryDayChargeReward.getTimeMark()).containsKey(userEveryDayChargeReward.getConditionId())) {
                    map.get(userEveryDayChargeReward.getActivityId()).get(userEveryDayChargeReward.getTimeMark()).put(userEveryDayChargeReward.getConditionId(), new ConcurrentHashMap<>());
                }
                map.get(userEveryDayChargeReward.getActivityId()).get(userEveryDayChargeReward.getTimeMark()).get(userEveryDayChargeReward.getConditionId()).put(userEveryDayChargeReward.getUserId(), userEveryDayChargeReward);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertUserEveryDayChargeRewardSql = "insert into t_u_every_day_charge_reward(activityId,userId,conditionId,timeMark,value,reward) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserEveryDayChargeReward userEveryDayChargeReward) {
        userEveryDayChargeReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userEveryDayChargeReward.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEveryDayChargeReward.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveryDayChargeReward.getConditionId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveryDayChargeReward.getTimeMark()));
        para.put(i++, new DbParameter(Types.VARCHAR, userEveryDayChargeReward.getValue().toString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEveryDayChargeReward.isReward()));
        if (getDb().executeNoneQuery(insertUserEveryDayChargeRewardSql, para) > 0) {
            return true;
        }
        return false;

    }

    public static String updateUserEveryDayChargeRewardSql = "update t_u_every_day_charge_reward set value=?,reward=? where activityId=? and userId=? and conditionId=? and timeMark=?";

    @Override
    protected boolean updateData(UserEveryDayChargeReward userEveryDayChargeReward) {
        userEveryDayChargeReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, userEveryDayChargeReward.getValue().toString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEveryDayChargeReward.isReward()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveryDayChargeReward.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEveryDayChargeReward.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveryDayChargeReward.getConditionId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveryDayChargeReward.getTimeMark()));
        if (getDb().executeNoneQuery(updateUserEveryDayChargeRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserEveryDayChargeReward parseData(ResultSet rs) throws SQLException {
        UserEveryDayChargeReward userEveryDayChargeReward = new UserEveryDayChargeReward();
        userEveryDayChargeReward.setActivityId(rs.getInt("activityId"));
        userEveryDayChargeReward.setUserId(rs.getLong("userId"));
        userEveryDayChargeReward.setConditionId(rs.getInt("conditionId"));
        userEveryDayChargeReward.setTimeMark(rs.getInt("timeMark"));
        userEveryDayChargeReward.setValue(new BigInteger(rs.getString("value")));
        userEveryDayChargeReward.setReward(rs.getBoolean("reward"));
        return userEveryDayChargeReward;
    }
}
